﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Wms;
using nERP.Model;

namespace nERP.ViewModel.Wms.OtherOutWarehouseVMs
{
    public partial class OtherOutWarehouseListVM : BasePagedListVM<OtherOutWarehouse_View, OtherOutWarehouseSearcher>
    {
        
        protected override IEnumerable<IGridColumn<OtherOutWarehouse_View>> InitGridHeader()
        {
            return new List<GridColumn<OtherOutWarehouse_View>>{
                
                this.MakeGridHeader(x => x.OtherOutWarehouse_OtherOutWarehouseCode).SetTitle(@Localizer["Page.出库单号"].Value),
                this.MakeGridHeader(x => x.OtherOutWarehouse_OtherOutType).SetTitle(@Localizer["Page.业务类别"].Value),
                this.MakeGridHeader(x => x.OtherOutWarehouse_MakeUser).SetTitle(@Localizer["Page.制单人"].Value),
                this.MakeGridHeader(x => x.OtherOutWarehouse_InvoicesDate).SetTitle(@Localizer["Page.单据日期"].Value),
                this.MakeGridHeader(x => x.OtherOutWarehouse_Status).SetTitle(@Localizer["Page.单据状态"].Value),
                this.MakeGridHeader(x => x.OtherOutWarehouse_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.OtherOutWarehouse_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.OtherOutWarehouse_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.OtherOutWarehouse_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.OtherOutWarehouse_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.OtherOutWarehouse_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),
                this.MakeGridHeader(x => "wtmtemp_CanApprove").SetHide().SetFormat((a, b) =>
                {
                    if(a.wtmtemp_CanApprove) { return "true"; }
                    else { return "false"; }
                }),
                

                this.MakeGridHeaderAction(width: 200)
            };
        }

        
        public override IOrderedQueryable<OtherOutWarehouse_View> GetSearchQuery()
        {
            var query = DC.Set<OtherOutWarehouse>()
                
                .CheckContain(Searcher.OtherOutWarehouseCode, x=>x.OtherOutWarehouseCode)
                .CheckEqual(Searcher.OtherOutTypeId, x=>x.OtherOutTypeId)
                .CheckEqual(Searcher.MakeUserId, x=>x.MakeUserId)
                .CheckBetween(Searcher.InvoicesDate?.GetStartTime(), Searcher.InvoicesDate?.GetEndTime(), x => x.InvoicesDate, includeMax: false)
                .CheckEqual(Searcher.Status, x=>x.Status)
                .CheckContain(Searcher.Remark, x=>x.Remark)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x=>x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x=>x.UpdateBy)
                .Select(x => new OtherOutWarehouse_View
                {
				    ID = x.ID,
                    
                    OtherOutWarehouse_OtherOutWarehouseCode = x.OtherOutWarehouseCode,
                    OtherOutWarehouse_OtherOutType = x.OtherOutType.DicFieldDes,
                    OtherOutWarehouse_MakeUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MakeUserId).Select(y => y.Name).FirstOrDefault(),
                    OtherOutWarehouse_InvoicesDate = x.InvoicesDate,
                    OtherOutWarehouse_Status = x.Status,
                    OtherOutWarehouse_Remark = x.Remark,
                    OtherOutWarehouse_CreateTime = x.CreateTime,
                    OtherOutWarehouse_UpdateTime = x.UpdateTime,
                    OtherOutWarehouse_CreateBy = x.CreateBy,
                    OtherOutWarehouse_UpdateBy = x.UpdateBy,
                    OtherOutWarehouse_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }

        
    public override void AfterDoSearcher()
    {
        if (Wtm.LoginUserInfo != null)
        {
            var ids = GetMyApproves();
            foreach (var item in EntityList)
            {
                var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                if (m!=null)
                {
                    item.wtmtemp_CanApprove = true;
                }
            }
        }
    }


    }
    public class OtherOutWarehouse_View: OtherOutWarehouse
    {
        
        public string OtherOutWarehouse_OtherOutWarehouseCode { get; set; }
        public string OtherOutWarehouse_OtherOutType { get; set; }
        public string OtherOutWarehouse_MakeUser { get; set; }
        public DateTime? OtherOutWarehouse_InvoicesDate { get; set; }
        public StatusTypeEnum? OtherOutWarehouse_Status { get; set; }
        public string OtherOutWarehouse_Remark { get; set; }
        public DateTime? OtherOutWarehouse_CreateTime { get; set; }
        public DateTime? OtherOutWarehouse_UpdateTime { get; set; }
        public string OtherOutWarehouse_CreateBy { get; set; }
        public string OtherOutWarehouse_UpdateBy { get; set; }
        public bool OtherOutWarehouse_IsValid { get; set; }
                public bool wtmtemp_CanApprove { get; set; }

    }

}